Imports Yvdh
Imports CIPF.Annotations
Imports CIPF.Numerical
Public Class SimpleAnnotatedImageViewer
  Public Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
  End Sub

  Public ReadOnly Property AnnotatedImage() As AnnotatedImage
    Get
      Return Me._annotatedImage
    End Get
  End Property

  Private Sub AnnotatedImageMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _annotatedImage.MouseMove
    ShowPixelInfo(New Point(e.X, e.Y))
  End Sub

  Private Sub AnnotatedImageViewer_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    RefreshStatusBar()
  End Sub

  Private Sub AnnotatedImageResize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _annotatedImage.Resize
    If Me.WindowState = FormWindowState.Normal Then
      Me.ClientSize = New Size(Math.Max(Me._annotatedImage.Width, Me.MinimumSize.Width), Math.Max(Me._annotatedImage.Height + stbImage.Height, Me.MinimumSize.Height))
      Debug.WriteLine("AnnotatedImageViewer received resize event from AnnotatedImage control", "AnnotatedImageResize")
    End If
  End Sub

  Private Sub ShowPixelInfo(ByVal clientCoordinates As Point)
    'Update the panel with the cursor position and pixel value in appropriate color space
    If AnnotatedImage.Bitmap Is Nothing Then Exit Sub

    Dim imageCoordinates As Point = Point.Round(Util.ClientToImageCoordinates(clientCoordinates, AnnotatedImage.DisplayZoom, AnnotatedImage.DisplayOffset))
    Dim imageRectangle As New Rectangle(0, 0, AnnotatedImage.Bitmap.Width, AnnotatedImage.Bitmap.Height)
    If imageRectangle.Contains(imageCoordinates) = False Then Exit Sub
    Dim strText As String = imageCoordinates.X & " ," & imageCoordinates.Y
    stbImage.Panels(2).Text = strText
  End Sub

  Private Sub RefreshStatusBar()
    'Show image information in the status bar, except cursor position and values
    With Me.AnnotatedImage
      If Not .Bitmap Is Nothing Then
        Dim oSize As Size = .BitmapSize

        If .Resolution > 0 Then
          stbImage.Panels(0).Text = oSize.Width & " x " & oSize.Height & ", " & _
              .ColorDepth & " bpp, " & _
              .Resolution.ToString("######.#") & " dpi"
        Else
          stbImage.Panels(0).Text = oSize.Width & " x " & oSize.Height & ", " & _
              .ColorDepth & " bpp, " & _
              .Resolution.ToString("#.#####") & " dpi"
        End If
        stbImage.Panels(1).Text = "Disp. = " & .DisplayZoom.ToString("##.##") & _
          ", phys. = " & .PhysicalZoom.ToString("##.##")
      End If
    End With
  End Sub

End Class